package com.ponhu.ea2.goodsCenter.provider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ponhu.ea2.entity.*;
import com.ponhu.ea2.goodsCenter.provider.mapper.MaterialMapper;
import com.ponhu.ea2.goodsCenter.provider.mapper.PhSeriesMapper;
import com.ponhu.ea2.goodsCenter.provider.service.MaterialService;
import com.ponhu.ea2.goodsCenter.provider.service.PhBrandService;
import com.ponhu.ea2.goodsCenter.provider.service.PhCategoryService;
import com.ponhu.ea2.goodsCenter.provider.service.PhModelService;
import com.ponhu.ea2.vo.PhMaterialVo;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * @author zhaokai
 */
@Service
public class MaterialServiceImpl
        extends ServiceImpl<MaterialMapper, Material>
        implements MaterialService {

    @Autowired
    private PhBrandService brandService;

    @Autowired
    private PhCategoryService categoryService;

    @Autowired
    private PhSeriesMapper seriesMapper;

    @Autowired
    private PhModelService modelService;

    @Override
    public Material getOneMaterialByCode(String materialCode) {
        LambdaQueryWrapper<Material> queryWrapper = Wrappers
                .lambdaQuery(Material.class)
                .eq(Material::getMaterialCode, materialCode);

        return getOne(queryWrapper);
    }

    /**
     * 根据物料编码查询物料信息
     *
     * @param materialCode
     * @return
     */
    @Override
    public List<Material> getMaterialByCode(String materialCode) {

        LambdaQueryWrapper<Material> queryWrapper = Wrappers
                .lambdaQuery(Material.class)
                .eq(Material::getMaterialCode, materialCode);

        return list(queryWrapper);
    }

    /**
     * 根据品牌或系列查询所有物料型号
     *
     * @param brandId
     * @param seriesId
     * @return
     */
    @Override
    public List<Material> getPhModelsByBrandOrSeries(Long brandId, Long seriesId) {

        LambdaQueryWrapper<Material> queryWrapper = Wrappers.lambdaQuery(Material.class);
        if (ObjectUtils.isNotEmpty(brandId)) {
            queryWrapper.eq(Material::getBrandId, brandId);
        }
        if (ObjectUtils.isNotEmpty(seriesId)) {
            queryWrapper.eq(Material::getSeriesId, seriesId);
        }

        return list(queryWrapper);
    }

    @Override
    public PhMaterialVo getMaterialVoByCode(String materialCode) {
        Material material = this.getOne(Wrappers.lambdaQuery(Material.class)
                .eq(Material::getMaterialCode, materialCode));

        PhMaterialVo materialVo = new PhMaterialVo();
        BeanUtils.copyProperties(material, materialVo);

        //设置品牌
        CompletableFuture<Void> brandFuture = CompletableFuture.runAsync(() -> {
            if (material.getBrandId() != null) {
                PhBrand brand = brandService.getById(material.getBrandId());
                if (brand != null) {
                    materialVo.setBrandCnName(brand.getCnName());
                    materialVo.setBrandEnName(brand.getEnName());
                }
            }
        });

        //设置品类
        CompletableFuture<Void> categoryFuture = CompletableFuture.runAsync(() -> {
            if (material.getCategoryId() != null) {
                PhCategory category = categoryService.getById(material.getCategoryId());
                if (category != null) {
                    materialVo.setThirdCategoryName(category.getName());
                }
            }
        });

        //设置系列
        CompletableFuture<Void> seriesFuture = CompletableFuture.runAsync(() -> {
            if (material.getSeriesId() != null) {
                PhSeries series = seriesMapper.selectById(material.getSeriesId());
                if (series != null) {
                    materialVo.setSeriesName(series.getName());
                }
            }
        });

        //设置型号
        CompletableFuture<Void> modelFuture = CompletableFuture.runAsync(() -> {
            if (material.getModelId() != null) {
                PhModel model = modelService.getById(material.getModelId());
                if (model != null) {
                    materialVo.setModelName(model.getModel());
                }
            }
        });

        //聚合异步任务
        CompletableFuture.allOf(brandFuture, categoryFuture, seriesFuture, modelFuture).join();

        return materialVo;
    }

    /**
     * 根据条件获取物料
     *
     * @param categoryId 三级分类Id
     * @param brandId    品牌
     * @param seriesId   系列Id
     * @param modelId    型号Id
     * @return
     */
    @Override
    public Material getMaterialByCondition(Long categoryId, Long brandId, Long seriesId, Long modelId) {

        return getOne(Wrappers.lambdaQuery(Material.class)
                .eq(Material::getCategoryId, categoryId)
                .eq(Material::getBrandId, brandId)
                .eq(Material::getSeriesId, seriesId)
                .eq(Material::getModelId, modelId).last("limit 1"));
    }

    /**
     * 根据物料编码更新物料信息
     *
     * @param material
     * @param materialCode
     * @return
     */
    @Override
    public boolean updateMaterialByMaterialCode(Material material, String materialCode) {
        return this.update(material, Wrappers.lambdaUpdate(Material.class)
                .eq(Material::getMaterialCode, materialCode));
    }

    @Override
    public List<Material> getMaterialsByCodes(List<String> materialCodes) {

        LambdaQueryWrapper<Material> queryWrapper = Wrappers
                .lambdaQuery(Material.class)
                .in(Material::getMaterialCode, materialCodes);

        return list(queryWrapper);
    }

}




